/**
 * 应用的核心js文件
 */
var grErp = angular.module('gr-erp', []);
grErp.filter('getarr',function(){
    return function(obj,propty){
        var arr = [];
        angular.forEach(obj,function(data,index,array){
            if(data.belong_to == propty){
                arr.push(data);
            }
        })
        return arr;
    }
});
grErp.controller('TestCtrl',['$scope',function($scope){
    $scope.checkVersion = function(){
        alert('hello there~');
    };
}]);
grErp.controller('StorageOutCtrl',['$scope',function($scope){
    $scope.items = [{inventory:"",cnt:"",id:""}];
    $scope.moreItem = function(){
        $scope.items.push({inventory:"",cnt:"",id:""});
    }
    $scope.init=function(o){
        $scope.product = o;
    }
    $scope.select = function($index,x){
        var proIndex = parseInt(x);
        if(isNaN(proIndex)){
            $scope.items[$index].inventory = "";
            return false;
        }
        $scope.items[$index].inventory = $scope.product[proIndex].cnt;
        $scope.items[$index].id = $scope.product[proIndex].id;
    }
    $scope.cnt = function($index){
        var cnt = $scope.items[$index].cnt;
        var inventory = $scope.items[$index].inventory;
        console.log(cnt,inventory);
        if(isNaN(cnt)&&cnt!= undefined){
            $scope.items[$index].cnt = 1;
            alert("请输入数字");
        }else if(cnt<1){
            $scope.items[$index].cnt = 1;
            alert("最少出库数量为1");
        }else if(Number(cnt)>Number(inventory)){
            $scope.items[$index].cnt = inventory;
            alert("没有这么多库存");
        }

    }
    $scope.removeItem = function($index){
        if(!confirm('是否确定删除?')) return;
        $scope.items.splice($index,1);
    }
    $scope.save = function(){
        alert(JSON.stringify($scope.items));
    }
}]);
grErp.controller('EditProductCtr',['$scope',function($scope){

}]);
grErp.directive('isAlreadyTaken', function() {
    return {
        require: 'ngModel',
        link: function(scope, ele, attrs, ctrl) {
            ctrl.$parsers.push(function(val) {
                ctrl.$setValidity('emailAvailable', false);
                var emailTable = [
                    'K@gmail.com',
                    'a@gmail.com',
                    'v@gmail.com',
                    'l@gmail.com',
                    'e@gmail.com',
                    'z@gmail.com'];
                for (var i=0;i<emailTable.length;i+=1){
                    if(val==emailTable[i]){
                        ctrl.$setValidity('emailAvailable', true);
                        return;
                    }
                }
                return val;
            })
        }
    }
});
grErp.directive('ensureUnique', ['$http', function($http) {
    return {
        require: 'ngModel',
        link: function(scope, ele, attrs, c) {
            ele.bind('focus', function(evt) {
                scope.$apply(function() {
                    c.focused = true;
                });
            }).bind('blur', function(evt) {
                scope.$apply(function() {
                    c.focused = false;
                    var val = ele.val();
                    if(!val){
                        c.$setValidity('unique', true);
                        return;
                    }
                    $http({
                        method: 'POST',
                        url: '/gr_erp/index.php?s=/Api/Product/index',
                        params:{spu:val}
                    }).success(function(data, status, headers, cfg) {
                        if(data != "null"){
                            //存在
                            c.$setValidity('unique', false);
                        }
                    }).error(function(data, status, headers, cfg) {
                        console.log(data);
                    });
                });
            });
        }
    }
}]);
grErp.directive('select2', function (select2Query) {
    return {
        restrict: 'A',
        scope: {
            config: '=',
            ngModel: '=',
            select2Model: '='
        },
        link: function (scope, element, attrs) {
            // 初始化
            var tagName = element[0].tagName,
                config = {
                    allowClear: true,
                    multiple: !!attrs.multiple,
                    placeholder: attrs.placeholder || ' '   // 修复不出现删除按钮的情况
                };

            // 生成select
            if(tagName === 'SELECT') {
                var $element = $(element);
                delete config.multiple;
                $element
                    .prepend('<option value=""></option>')
                    .val('')
                    .select2(config);
                return false;
            }
        }
    }
});
grErp.factory('select2Query', function ($timeout) {
    return {
    }
});
grErp.controller('RejectCtrl',['$scope',function($scope){
    $scope.items = [{inventory:"",cnt:"",id:""}];
    $scope.moreItem = function(){
        $scope.items.push({inventory:"",cnt:"",id:""});
    }
    $scope.init=function(o){
        $scope.product = o;
    }
    $scope.select = function($index,x){
        var proIndex = parseInt(x);
        if(isNaN(proIndex)){
            return false;
        }
        $scope.items[$index].id = $scope.product[proIndex].id;
    }
    $scope.removeItem = function($index){
        if(!confirm('是否确定删除?')) return;
        $scope.items.splice($index,1);
    }

}]);
grErp.controller('SubproInCtrl',['$scope',function($scope){
    $scope.items = [{}];
    $scope.moreItem = function(){
        $scope.items.push({});
    }
    $scope.init=function(o){
        $scope.product = o;
    }
    $scope.select = function($index,x){
        var proIndex = parseInt(x);
        if(isNaN(proIndex)){
            $scope.items[$index].inventory = "";
            return false;
        }
        $scope.items[$index].inventory = $scope.product[proIndex].cnt;
        $scope.items[$index].id = $scope.product[proIndex].id;
        $scope.items[$index].spec = $scope.product[proIndex].spec;
    }
    $scope.removeItem = function($index){
        if(!confirm('是否确定删除?')) return;
        $scope.items.splice($index,1);
    }
    $scope.save = function(){
        alert(JSON.stringify($scope.items));
    }
    $scope.autocount = function($index){
        if($scope.items[$index].cnt>0){
            if($scope.items[$index].unitprice > 0){
                $scope.items[$index].totalprice = $scope.items[$index].cnt * $scope.items[$index].unitprice;
            }
            if($scope.items[$index].totalprice > 0){
                $scope.items[$index].unitprice = $scope.items[$index].totalprice/$scope.items[$index].cnt;

            }
            $scope.items[$index].unitprice = parseFloat($scope.items[$index].unitprice).toFixed(3);
        }
    }
}]);
grErp.controller('SubproOutCtrl',['$scope',function($scope){
    $scope.items = [{}];
    $scope.moreItem = function(){
        $scope.items.push({});
    }
    $scope.init=function(o){
        $scope.product = o;
    }
    $scope.select = function($index,x){
        var proIndex = parseInt(x);
        if(isNaN(proIndex)){
            $scope.items[$index].inventory = "";
            return false;
        }
        $scope.items[$index].inventory = $scope.product[proIndex].cnt;
        $scope.items[$index].id = $scope.product[proIndex].id;
    }
    $scope.removeItem = function($index){
        if(!confirm('是否确定删除?')) return;
        $scope.items.splice($index,1);
    }
    $scope.cnt = function($index){
        console.log($index);
        if($scope.items[$index].cnt>$scope.items[$index].inventory){
            $scope.items[$index].cnt="";
            alert("数量过大");
        }
    }
}]);
grErp.controller('StorageInCtrl',['$scope',function($scope){
    $scope.items = [{}];
    $scope.moreItem = function(){
        $scope.items.push({});

    }

    $scope.init=function(o){
//        alert(o);
        $scope.product = o;
    }
    $scope.select = function($index,x){
        var proIndex = parseInt(x);
        if(isNaN(proIndex)){
            $scope.items[$index].inventory = "";
            return false;
        }
        $scope.items[$index].id = $scope.product[proIndex].id;
    }



    $scope.removeItem = function($index){
        if(!confirm('是否确定删除?')) return;
        $scope.items.splice($index,1);
    }

    $scope.save = function(){
        alert(JSON.stringify($scope.items));
    }
}]);
grErp.controller('ProCtrl',['$scope','$filter',function($scope,$filter){
    $scope.band = "无";
    $scope.band_no = '00';
    $scope.init =function(classify,fruitclassify,pname,band){
        $scope.cas = classify;
        $scope.fas = fruitclassify;
        $scope.pas = pname;
        $scope.bas = band;
        $scope.apas=[];
    }
    $scope.caselect = function($index){
        c = $scope.cas[$index];
        console.log($scope.cas[$index].classify);
        $scope.classify = c.classify;
        $scope.classify_no = c.classify_no;
    }
    $scope.faselect = function($index){
        f = $scope.fas[$index];
        $scope.fruit_classify = f.fruit_classify;
        $scope.fruit_classify_no = f.fruit_classify_no;
        $scope.apas = $filter('getarr')($scope.pas,$scope.fruit_classify_no);
        $scope.abas =  $filter('getarr')($scope.bas,$scope.fruit_classify_no);
        $scope.pname = "";
        $scope.pname_no = "";
        $scope.band = "无";
        $scope.band_no = '00';
    }
    $scope.apaselect = function($index){
        if(parseInt($index)<0){
            $scope.pname = "";
            $scope.pname_no = "";
            return false;
        }
        p = $scope.apas[$index];
        $scope.pname = p.pname;
        $scope.pname_no = p.pname_no;
    }
    $scope.abaselect = function($index){
        if(parseInt($index)<0){
            $scope.band = "无";
            $scope.band_no = '00';
            return false;
        }
        b = $scope.abas[$index];
        $scope.band = b.band;
        $scope.band_no = b.band_no;
    }
    $scope.createbin = function(){

        $scope.BIN = String($scope.classify_no)+String($scope.fruit_classify_no)+String($scope.pname_no)+String($scope.band_no);
    }

}]);
grErp.controller('InventoryCtrl',['$scope','$filter',function($scope,$filter){
    $scope.items = [{BIN:'初始化'}];
    $scope.moreItem = function(){
        $scope.items.push({BIN:'初始化'});
    }
    $scope.init=function(o,i){
        $scope.product = o;
        $scope.b_invens = i;
    }
    $scope.addproselect = function($index,x){

        BIN = x;
        $scope.items[$index].BIN = x;
    }
    $scope.outproselect = function($index,x){
        BIN = x;
        p = $scope.product[x];
        $scope.items[$index].BIN = p.BIN;
    }
    $scope.outbatchoselect = function($index,wb){
        wb = JSON.parse(wb);
        $skip = true;
        angular.forEach($scope.items,function(data,index,array){
            if(data.warnbatch == wb.warn_batch&&wb.BIN == data.BIN){
                $scope.items[$index].inventory = "";
                $scope.items[$index].warnbatch = "";
                $scope.items.splice($index,1);
                alert('相同商品相同预警批次存在，已删除');
                $skip = false;
            }
        });
        if($skip){
            $scope.items[$index].inventory = parseInt(wb.cnt);
            $scope.items[$index].warnbatch = wb.warn_batch;
        }

    }
    $scope.cnt = function($index){
        i = $scope.items[$index].inventory
        if(i == ""||i == undefined){
            $scope.items[$index].cnt="";
            alert("请重新选择出库商品和批次");
            return false;
        }

        if($scope.items[$index].cnt>$scope.items[$index].inventory){
            $scope.items[$index].cnt="";
            alert("数量过大,重新输入");
            return false;
        }
    }
    $scope.removeItem = function($index){
        if(!confirm('是否确定删除?')) return;
        $scope.items.splice($index,1);
    }
}]);